/*
 * AJAX FUNCTIONS
*/

/**
 * creates XMLHttpRequest
*/
function createRequest() 
{
  try 
  {
    request = new XMLHttpRequest();
  } 
  catch (trymicrosoft) 
  {
    try 
	{
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (othermicrosoft) 
	{
      try 
	  {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } 
	  catch (failed) 
	  {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}

/**
 * creates many XMLHttpRequest
*/
function createRequests() 
{
   try 
  {
      this.req = new XMLHttpRequest();
  } 
  catch (trymicrosoft) 
  {
    try 
	{
      this.req = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (othermicrosoft) 
	{
      try 
	  {
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
      } 
	  catch (failed) 
	  {
        this.req = false;
      }
    }
  }

  if (!this.req)
    alert("Error initializing XMLHttpRequest!");
}

/**
 * executes server function by given url
 * param string url
*/
function SendValues(url)
{
	createRequest();
	var url = url;
	request.open("GET", url, false);
	request.onreadystatechange = null;
	request.send(null);	
}

/**
 * executes server function by given url and reloads the page
 * param string url
*/
function SendValuesWithPageReload(url)
{
	createRequest();
	var url = url;
	request.open("GET", url, true);
	request.onreadystatechange = function()
	{
		if (request.readyState == 4)
	 		if (request.status == 200) 
			{
				window.location.reload();
			}
	}	
	request.send(null);	
}

/**
 * gets data from server and inserts in the tag with the given id
 *
 * param string url url for the server operation
 * param string id of the field to update
*/
function GetAjaxValues(url, id)
{
createRequest();
request.onreadystatechange = function()
{
	if (request.readyState == 4)
		if (request.status == 200) 
		{
			response = request.responseText.split("@");
		    document.getElementById(id).parentNode.innerHTML = response[1];
		
		}
}
request.open("GET", url, true);
request.send(null);
}

/**
 * gets data from server and inserts in the tag with the given id
 *
 * param string url url for the server operation
 * param string id of the field to update
 * param string show_elem_id id of that element we want to reveal
*/
function GetAjaxValuesAndPaste(url, id, show_elem_id)
{
createRequest();
request.onreadystatechange = function()
{
	if (request.readyState == 4)
		if (request.status == 200) 
		{
			response = request.responseText.split("@");
		    document.getElementById(id).parentNode.innerHTML = response[1];
		    document.getElementById(show_elem_id).style.display = '';
		
		}
}
request.open("GET", url, true);
request.send(null);
}

function GetLogs(url)
{
	var tables = document.getElementsByTagName('table');
	var flag = 0;
	
	for (i = 0; i < tables.length; i++)
	{
		if(tables[i].id=='logs_table')
		{
			if (tables[i].style.display == 'none')
			{		
				tables[i].style.display = '';
			}
			else 
			{
				tables[i].style.display = 'none';
			}
			flag++;
		}
	}	
	
	if (flag==0)
	{
		createRequest();
		var url = url;
		request.open("GET", url, true);
		request.onreadystatechange = DisplayLogs;
		request.send(null);
	}
}

function DisplayLogs()
{
	if (request.readyState == 4)
	 	if (request.status == 200) 
		{
			response = request.responseText.split("@");
			document.getElementById("logs").innerHTML = response[1];
		}
}

function GetHtmlElement(url,id,function_name)
{
	var request = new Array()
	request[id] = new createRequests();
	request[id].req.onreadystatechange = function()
	{
	if (request[id].req.readyState == 4)
		if (request[id].req.status == 200) 
		{
			response = request[id].req.responseText.split("@");
			
		    document.getElementById(id).parentNode.innerHTML = response[1];
		    if(function_name!=null)
		    eval(function_name);
		}
	}
	request[id].req.open("GET", url, true);
	request[id].req.send(null);
}

function move_to(url,myform)
{
    createRequest();
    request.open("GET", url, true);
    request.onreadystatechange = function()
    {
		if (request.readyState == 4)
	 		if (request.status == 200) 
			{
				document.forms[myform].submit();
			}
    }
    request.send(null);
}

function GetThemePicture(url)
{
	createRequest();
	var url = url;
	request.open("GET", url, true);
	request.onreadystatechange = DisplayThemeImage;
	request.send(null);
}

function DisplayThemeImage()
{
	if (request.readyState == 4)
	 	if (request.status == 200) 
		{
			response = request.responseText.split("@");
			document.getElementById("themetd").innerHTML = response[1];
		}
}